package quartz;

import com.alibaba.fastjson.JSON;
import com.tc.quartz.support.bean.SystemDictBean;
import com.tc.quartz.support.quartz.JobClassFactory;
import com.tc.quartz.support.quartz.VpalSchedulerFactory;
import com.tc.quartz.support.service.ISystemDictService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring-quartz.xml" }, inheritLocations = true)
@Slf4j
public class test {
    @Autowired
    private ISystemDictService systemDictService;


    @Test
    public void demo(){
        List<SystemDictBean> dict = systemDictService.getDict("", "");
        log.info(JSON.toJSONString(dict));
    }

    @Resource(name="quartzDataSourceProperties")
    private Map<String, String> quartzDataSourceProperties;

    @Test
    public void addJob() throws Exception{
        String schedulerName = "schedulerName-Cap-1";
        String jobName = "job-name-recon";
        String jobGroup = Scheduler.DEFAULT_GROUP;
        String jobclassName = "com.tc.quartz.server.job.DisallowConcurrentProxyJob";
//		String jobclassName = "com.vip.vpal.quartz.server.job.TestJob";
        String jobDescription = "对账跑批";
        String triggerCron = "*/50 * * * * ?"; // "*/20 1 1 * * ?";
        String triggerSuffix = "_trigger";

        JobDataMap dmap = new JobDataMap();
        dmap.put("PROXY_CLASS_NAME", "com.tc.service.UserInfoSerivce");
        dmap.put("PROXY_METHOD_NAME", "healthCheck");

        Scheduler scheduler = null;
        try {
            scheduler = VpalSchedulerFactory.getVpalScheduler(schedulerName, quartzDataSourceProperties);
            Class<? extends Job> clazz = JobClassFactory.getClassByName(jobclassName);
            JobDetail jobDetail = JobBuilder.newJob(clazz).withDescription(jobDescription).withIdentity(jobName, jobGroup).usingJobData(dmap).build();

            CronTrigger trigger = (CronTrigger) TriggerBuilder.newTrigger()
                    .withDescription(jobDescription + triggerSuffix).startAt(new Date()).endAt(null)
                    .withIdentity(jobName + triggerSuffix, jobGroup).withSchedule(CronScheduleBuilder.cronSchedule(triggerCron)).build();

            scheduler.scheduleJob(jobDetail, trigger);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            scheduler.shutdown();
        }
        System.out.println(" JobAddTest ok ... ");
    }

    @Test
    public void delJob() throws Exception{
        String schedulerName = "schedulerName-Cap-1";
        String jobName = "job-name-recon";
        String jobGroup = Scheduler.DEFAULT_GROUP;

        Scheduler scheduler = null;
        try {
            scheduler = VpalSchedulerFactory.getVpalScheduler(schedulerName, quartzDataSourceProperties);
            JobKey jobKey = new JobKey(jobName, jobGroup);
            boolean deleteResult = scheduler.deleteJob(jobKey);
            System.out.println("JOB删除结果：" + deleteResult);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            scheduler.shutdown();
        }
        System.out.println(" JobDelTest ok ... ");
    }


}
